package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.AccidentAudit;
import cn.wolfcode.crm.domain.Damage;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.AccidentAuditQueryObject;
import cn.wolfcode.crm.service.IAccidentAuditService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.LogicException;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.UserContext;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("accidentAudit")
public class AccidentAuditController {

    @Autowired
    private IAccidentAuditService accidentAuditService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"accidentAudit:view"," 理赔审核页面"},logical = Logical.OR)
    public String view() {
        return "accidentAudit";
    }

    @RequestMapping("query")
    @ResponseBody
    @RequiresPermissions(value = {"accidentAudit:query","理赔审核列表"},logical = Logical.OR)
    public PageResult query(AccidentAuditQueryObject qo) {
        return accidentAuditService.query(qo);
    }

    @RequestMapping("changeState")
    @ResponseBody
    public JsonResult changeState(Long id) {
        try {
            //录入审核人和时间
            Employee currentUser = UserContext.getCurrentUser();
            AccidentAudit accidentAudit = accidentAuditService.selectByPrimaryKey(id);
            accidentAudit.setAuditTime(new Date());
            accidentAudit.setAuditUser(currentUser);
            //改变审核状态
            accidentAuditService.updateByAudit(accidentAudit);
            accidentAuditService.changeState(id);
            return JsonResult.success("");
        }
        catch (LogicException e) {
            e.printStackTrace();
            return JsonResult.mark(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return JsonResult.mark("操作失败");
        }
    }

    @RequestMapping("getDamageItemByClaimId")
    @ResponseBody
    @RequiresPermissions(value = {"accidentAudit:getDamageItemByClaimId","理赔审核列表"},logical = Logical.OR)
    public Damage getDamageItemByClaimId(String staging) {
        return accidentAuditService.getDamageItemByClaimId(staging);
    }
}
